package com.angel.controller;

import com.angel.bean.AppInfo;
import com.angel.bean.OutWebResourceUrl;
import com.angel.bean.UserInfo;
import com.angel.service.AppInfoService;
import com.angel.service.OutWebResourceUrlService;
import com.angel.shiro.SelFormAuthenticationFilter;
import com.angel.shiro.SystemAdminAuthorizingRealm;
import com.angel.service.SysMenuService;
import com.angel.shiro.UserAuthorizingRealm;
import com.angel.utils.AdminUserUtils;
import com.angel.utils.EmployeeUtils;
import com.angel.utils.StringUtils;
import com.angel.utils.UserUtils;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 管理员登录控制器
 *
 * @author Rabit
 */
@Controller
public class LoginController {
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private SysMenuService menuService;

    @Autowired
    private OutWebResourceUrlService outWebResourceUrlService;

    @Autowired
    private AppInfoService appInfoService;

   /* *//**
     * 进入登录界面
     *
     * @return
     *//*
    @GetMapping("/login")
    public String login() {
        //记录登录日志
        logger.info("---------------------web有admin又要登录了");
        return "login";
    }*/

    @GetMapping("/userlogin")
    public ModelAndView userLogin() {
        //记录登录日志
        logger.info("---------------------web有vip又要登录了");
        ModelAndView mv = new ModelAndView("userlogin");
        return mv;
    }

    /**
     * 登录成功，进入管理界面 需要用户权限
     *
     * @return
     */
    @GetMapping("/index")
    public ModelAndView adminIndex(Model model, HttpSession session) {
        //数据初始化
        //获取会员激活链接
        AppInfo appInfo = new AppInfo();
        appInfo.setId(1);
        UserInfo userInfo = UserUtils.getPrincipal().getUserInfo();
        OutWebResourceUrl outWebResourceUrl = new OutWebResourceUrl();
        outWebResourceUrl.setId(9);
        outWebResourceUrl = outWebResourceUrlService.findOneOutWebResourceUrl(outWebResourceUrl);
        appInfo = appInfoService.findOne(appInfo);
        session.setAttribute("activeurl", outWebResourceUrl);
        session.setAttribute("user",userInfo);
        model.addAttribute("appInfo",appInfo);
        model.addAttribute("adminName", userInfo.getUserName());
        ModelAndView mv = new ModelAndView("index", "menuModel", model);
        return mv;
    }


    /**
     * 测试vip用户登录跳转到不通页面
     *
     * @return
     */
    @GetMapping("/userindex")
    public ModelAndView userIndex() {
        ModelAndView mv = new ModelAndView("upload");
        return mv;
    }


    /**
     * 登录失败，真正登录的POST请求由Filter完成
     *
     * @param request
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        UserAuthorizingRealm.Principal principal =  UserUtils.getPrincipal();

        // 如果已经登录，则跳转到管理首页
        if (principal != null) {
            this.logger.info(principal.getName() + ":登录了");
            return "redirect:" + "/index";
        }

        String username = WebUtils.getCleanParam(request, SelFormAuthenticationFilter.DEFAULT_USERNAME_PARAM);
        String message = (String) request.getAttribute(SelFormAuthenticationFilter.DEFAULT_MESSAGE_PARAM);

        if (StringUtils.isBlank(message) || StringUtils.equals(message, null)) {
            message = "用户或密码错误, 请重试.";
        }
        return "login";
    }


}
